using System.Collections;

using LocalBoard.Domain;

namespace LocalBoard.Web.UserControls
{
	using System;
	using System.Data;
	using System.Drawing;
	using System.Web;
	using System.Web.UI.WebControls;
	using System.Web.UI.HtmlControls;

	/// <summary>
	///		Summary description for RateArticle.
	/// </summary>
	public partial class RateArticle : MessageUserControl
	{

		protected void Page_Load(object sender, System.EventArgs e)
		{
			if(LoadCurrentMessage())
			{
				if(!boardController.GetBoardSecurity(ClubUser).CanUserRateArticle(_currentMessage))
				{
					rateForm.Visible = false;
					ratingNotAvailableLabel.Visible = true;
				}
				ArrayList voteList = boardController.GetRateListByMessageId(_currentMessageId);
				if(voteList.Count > 0)
				{
					int totalRate = 0;
					foreach(Vote vote in voteList)
					{
						totalRate += vote.Point;
					}
					int averageRate = totalRate / voteList.Count;

					rating.Text = averageRate.ToString();
					voteCount.Text = voteList.Count.ToString();
					ArrayList rateArray = new ArrayList();
					for(int i=1;i<=averageRate;i++)
					{
						rateArray.Add(i);
					}
					ratingImage.DataSource = rateArray;
					DataBind();
				}
				else
				{
					rateDisplay.Visible = false;
					notYetRatedLabel.Visible = true;
				}
			}
		}

		#region Web Form Designer generated code
		override protected void OnInit(EventArgs e)
		{
			//
			// CODEGEN: This call is required by the ASP.NET Web Form Designer.
			//
			InitializeComponent();
			base.OnInit(e);
		}
		
		/// <summary>
		///		Required method for Designer support - do not modify
		///		the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{

		}
		#endregion

		protected void rateIt_Click(object sender, System.EventArgs e)
		{
			if(LoadCurrentMessage() && rateNumbers.SelectedValue.Length > 0)
			{
				boardController.RateMessage(_currentMessageId, ClubUser.Id, Convert.ToInt32(rateNumbers.SelectedValue));
				Response.Redirect(Request.RawUrl);
			}
		}
	}
}
